var SZB = SZB || {};

function gather_get_sub_type(){
	var html = document.getElementById('cms_output_nav').innerHTML;
	html = html.replaceAll('</a>','');
        html = html.replaceAll('</A>','');
	html = html.substring(html.lastIndexOf('>')+1);
	return html;
}
                
function gather_get_title(){
	return $('#cms_output_title').html();
}

function gather_get_type(){
	return $('#cms_output_columntype').html();
}

function gather_get_intro(){
	return $('#cms_output_intro').html();
}

function gather_get_obj_id(){
    
        if(window.obj_id){
            return window.obj_id;
        }
        
	var url = window.location.href;
	var arr = url.split("/");
	var str = arr[arr.length-1];
	if (str.indexOf('.') > -1)
		str = str.substring(0,str.indexOf('.'));
	return str;
}

//为转发服务  可能来自全媒体或者来自理财产品详情页，或公告等
function gather_get_obj_id_for_forward(){
    if (window.obj_id) return window.obj_id;
	var url = window.location.href;
	var arr = url.split("/");
	var str = arr[arr.length-1];
	return str;
}
                
function is_member_article(){
	var loc = window.location.href;
	if(loc.indexOf('v_fortune/sft_jbsjlb/tbs_cfdj')!==-1 || loc.indexOf('v_fortune/sft_jbsjlb/tbs_lcnc') !== -1){
		return true;
	}
	return false;
}

function load_common_script(){
	var url = 'https://news.cnstock.com/script/index?cb=?';

	$.getJSON(url,{},function(script){
		if($('#cnstock_common_header').length>0 && $('#cnstock_common_header').html()==''){
			$('#cnstock_common_header').html(script.header);
		}

		if($('#cnstock_common_footer').length>0 && $('#cnstock_common_footer').html()==''){
			$('#cnstock_common_footer').html(script.footer);
		}
	});
}
				
var keyStr = "ABCDEFGHIJKLMNOP" +
   "QRSTUVWXYZabcdef" +
   "ghijklmnopqrstuv" +
   "wxyz0123456789+/" +
   "=";

function encode64(input) 
{
	input = escape(input);
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;

	do {
	   chr1 = input.charCodeAt(i++);
	   chr2 = input.charCodeAt(i++);
	   chr3 = input.charCodeAt(i++);

	   enc1 = chr1 >> 2;
	   enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	   enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	   enc4 = chr3 & 63;

	   if (isNaN(chr2)) {
		  enc3 = enc4 = 64;
	   } else if (isNaN(chr3)) {
		  enc4 = 64;
	   }

	   output = output +
		  keyStr.charAt(enc1) +
		  keyStr.charAt(enc2) +
		  keyStr.charAt(enc3) +
		  keyStr.charAt(enc4);
	   chr1 = chr2 = chr3 = "";
	   enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);

	return output;
 }

/*
document.body.oncopy = function(){
    
	if( !-[1,] ){
        event.returnValue = false;

        var t = document.selection.createRange().text;
        var s = "(\u672c\u6587\u6765\u81ea\uff1a\u4e2d\u56fd\u8bc1\u5238\u7f51 , \u8be6\u7ec6\u51fa\u5904\u53c2\u8003:"+window.location.href+")\n";
        
        t = t.replaceAll(/\n/ig,s);
        clipboardData.setData('Text' , t);
    }
};*/

SZB.articleJoinAction = function(action_title){
	this.joinAction = function(e){
		e.preventDefault();
                var url = appUrl + '/api/widgets/chk_kingclub_member_status?cb=?';
                $.getJSON(url,{},function(result){
                    if(result.error==0){
                    var url = appUrl+'/api/widgets/kingclub_action_apply'+'?action_title='+encodeURIComponent(action_title)+'&action_link='+encodeURIComponent(window.location.href); // 测试地址
                    //创建一个iframe
                    var iframe = $('<iframe style="display:none" id="join-frame" src="' + url + '" scrolling="no" frameborder="0" height="280" width="620"></frame>')
                    iframe.appendTo('body');
                    $.artDialog({
                            content:document.getElementById('join-frame'),
                            padding:0,
                            lock:true,
                            drag:false,
                            resize:false,
                            opacity:0
                    });
                    }else{
                        if(result.error == -1){
                            if(window.confirm('\u60a8\u5c1a\u672a\u767b\u5f55\uff0c\u7acb\u523b\u767b\u5f55?')){
                                cnstockLogin();
                            }
                        }else if(result.error == -2){
                            if(window.confirm('\u60a8\u8fd8\u4e0d\u662f\u91d1\u535a\u58eb\u4ff1\u4e50\u90e8\u4f1a\u5458,\u7acb\u523b\u52a0\u5165\uff1f')){
                                window.location.href = wealthUrl + '/king_club/join';
                            }
                        }
                    }
                });
	};
	$(document).on('click','[data-target="join"]',$.proxy(this.joinAction,this));
};

$(document).ready(function() {
    
	if($('#cnstock_common_header').length > 0 && $('#cnstock_common_header').html() == ''){
		load_common_script();
	}
        
        var location = window.location.href ? window.location.href  : '';
        if( location.indexOf('sft_jbsjlb/tbs_hdbd/fjhd_hdtt') !== -1 ){
            var action_title = $('h1').html();
            if(action_title && action_title.indexOf('join') == -1){
                title = action_title + '<a href="javascript:void(0);" data-target="join" class="join">\u7acb\u523b\u62a5\u540d</a>';
                $('h1').html(title);
                SZB.articleJoinAction(action_title);
            }
        }


    if($('.J_time_zone').length > 0){
		$('.J_time_zone').Zebra_DatePicker();
	}
 	

	var autoStockCode = function(id){

		if($("#"+id).length > 0){
			//var stockinput = $('#suggest');
			$("#"+id).autocomplete('https://companyapi.cnstock.com/companyAPI/getCompanyInfocode.do', {
				width:170,
				scrollHeight:300,
				multiple: false,
				matchContains: true,
				dataType: 'jsonp',
				cacheLength: 0,
				parse:function(data){
					var parsed = [];
					$.each(data, function(i,d){
						if(i=='rst'){
							for (var i = 0; i < d.length; i++) {
								parsed[parsed.length] = {
									data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
									value: d[i][1],
									result: d[i][1].replace(/(<.+?>)/gi, '')
								};
							}
						}
					});
					return parsed;
				},
				formatItem: function(row, i, n) {
					return row;     
				}
			});

			$("#"+id).result(function(event, data, formatted) {
				var hidden = $(this).parent().next().find(">:input");
				hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
				dojumpcode(id);
			});
		}
	};
	var autoStockCodeHk = function(id){

		if($("#"+id).length > 0){
			//var stockinput = $('#suggest');
			$("#"+id).autocomplete('https://companyapi.cnstock.com/companyAPI/getCompanyInfocode.do', {
				width:170,
				scrollHeight:300,
				multiple: false,
				matchContains: true,
				dataType: 'jsonp',
				cacheLength: 0,
				parse:function(data){
					var parsed = [];
					console.log(data);
					$.each(data, function(i,d){

						if(i=='rst'){
							for (var i = 0; i < d.length; i++) {
								parsed[parsed.length] = {
									data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
									value: d[i][1],
									result: d[i][1].replace(/(<.+?>)/gi, '')
								};
							}
						}
					});
					return parsed;
				},
				formatItem: function(row, i, n) {
					return row;     
				}
			});

			$("#"+id).result(function(event, data, formatted) {
				var hidden = $(this).parent().next().find(">:input");
				hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
				dojumpcode(id);
			});
		}
	};
	var autoStockCodeJXC = function(id){

		if($("#"+id).length > 0){
			//var stockinput = $('#suggest');
			$("#"+id).autocomplete('https://companyapi.cnstock.com/companyAPI/getFeaturedInfoCode.do', {
				width:170,
				scrollHeight:300,
				multiple: false,
				matchContains: true,
				dataType: 'jsonp',
				cacheLength: 0,
				parse:function(data){
					var parsed = [];
					console.log(data);
					$.each(data, function(i,d){

						if(i=='rst'){
							for (var i = 0; i < d.length; i++) {
								parsed[parsed.length] = {
									data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
									value: d[i][1],
									result: d[i][1].replace(/(<.+?>)/gi, '')
								};
							}
						}
					});
					return parsed;
				},
				formatItem: function(row, i, n) {
					return row;     
				}
			});

			$("#"+id).result(function(event, data, formatted) {
				var hidden = $(this).parent().next().find(">:input");
				hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
				dojumpcode(id);
			});
		}
	};
	var autoStockCodeGg = function(id){

		if($("#"+id).length > 0){
			//var stockinput = $('#suggest');
			$("#"+id).autocomplete('https://companyapi.cnstock.com/companyAPI/getCompanyInfocode.do', {
				width:170,
				scrollHeight:300,
				multiple: false,
				matchContains: true,
				dataType: 'jsonp',
				cacheLength: 0,
				parse:function(data){
					var parsed = [];
					$.each(data, function(i,d){
						if(i=='rst'){
							for (var i = 0; i < d.length; i++) {
								parsed[parsed.length] = {
									data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
									value: d[i][1],
									result: d[i][1].replace(/(<.+?>)/gi, '')
								};
							}
						}
					});
					return parsed;
				},
				formatItem: function(row, i, n) {
					return row;     
				}
			});

			$("#"+id).result(function(event, data, formatted) {
				var hidden = $(this).parent().next().find(">:input");
				hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
				dojumpcodegg(id);
			});
		}
	};	
	new autoStockCode('suggest');
	new autoStockCode('suggest2');
	new autoStockCode('suggest3');
	new autoStockCodeGg('suggest4');
	new autoStockCodeHk('suggestHk');
	new autoStockCodeHk('suggestHk2');
	new autoStockCodeHk('suggestHk3');
	new autoStockCodeHk('suggestHk4');
	new autoStockCodeJXC('suggestJXC');

	//为公告解读服务
	if($("#ggjd_suggest").length > 0){
		//var stockinput = $('#ggjd_suggest');
		$("#ggjd_suggest").autocomplete('https://companyapi.cnstock.com/companyAPI/getCompanyInfocode.do', {
			width:170,
			scrollHeight:300,
			multiple: false,
			matchContains: true,
			dataType: 'jsonp',
			cacheLength: 0,
			parse:function(data){
				var parsed = [];
				$.each(data, function(i,d){
					if(i=='rst'){
						for (var i = 0; i < d.length; i++) {
							parsed[parsed.length] = {
								data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
								value: d[i][1],
								result: d[i][1].replace(/(<.+?>)/gi, '')
							};
						}
					}
				});
				return parsed;
			},
			formatItem: function(row, i, n) {
				return row;     
			}
		});
	}
        

	//为导航条上的行情搜索服务
	if($("#J_search_input").length > 0){
		//var stockinput = $('#nav_keywords');
		$("#J_search_input").autocomplete('https://companyapi.cnstock.com/companyAPI/getCompanyInfocode.do', {
			width:170,
			scrollHeight:300,
			multiple: false,
			matchContains: true,
			dataType: 'jsonp',
			cacheLength: 0,
			parse:function(data){
				var parsed = [];
				$.each(data, function(i,d){
					if(i=='rst'){
						for (var i = 0; i < d.length; i++) {
							parsed[parsed.length] = {
								data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
								value: d[i][1],
								result: d[i][1].replace(/(<.+?>)/gi, '')
							};
						}
					}
				});
				return parsed;
			},
			formatItem: function(row, i, n) {
				return row;     
			}
		});

		$("#J_search_input").result(function(event, data, formatted) {
			var hidden = $(this).parent().next().find(">:input");
			hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
			dojumpcode('J_search_input');
		});
	}
	
	//会员专用文章
	if(is_member_article())
	{
		window.location.href = appUrl+'/page?s='+encode64(window.location.href);
		return false;
	}

	if( $('#cms_output_nav').html() && is_hit() ){ //判定文章页
                
		var obj_id = gather_get_obj_id();
		var obj_type = 'qmt';
		var type = encodeURIComponent( gather_get_type() )
		var sub_type = encodeURIComponent( gather_get_sub_type());
		
		var title = encodeURIComponent( gather_get_title() );
		var link = encodeURIComponent( window.location.href );
		var intro = encodeURIComponent( gather_get_intro() );

		var url = appUrl+'/api/gather/index?cb=?';
		$.getJSON(url,{'obj_id':obj_id,'obj_type':obj_type,'type':type,'sub_type':sub_type,'title':title,'link':link,'extra[intro]':intro},function(){});
	}
});


var flag = true;
function stockTipSuggest()
{
        if (flag == false) {
            return false;
        }
        //flag = false;
	//悬浮窗里的股票搜索
	if($("#suggest_tip").length > 0){
		$("#suggest_tip").autocomplete('https://companyapi.cnstock.com/companyAPI/getCompanyInfocode.do', {
			width:170,
			scrollHeight:300,
			multiple: false,
			matchContains: true,
			dataType: 'jsonp',
			cacheLength: 0,
			parse:function(data){
                            var parsed = [];
                            $.each(data, function(i,d){
                                if(i=='rst'){
                                    for (var i = 0; i < d.length; i++) {
                                        parsed[parsed.length] = {
                                            data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
                                            value: d[i][1],
                                            result: d[i][1].replace(/(<.+?>)/gi, '')
                                        };
                                    }
                                }
                            });
                            return parsed;
			},
			formatItem: function(row, i, n) {
				return row;     
			}
		});
      
		$("#suggest_tip").result(function( event, data, formatted ) {
			var hidden = $(this).parent().next().find(">:input");
			hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
			dojumpcode('suggest_tip');
		});
	}
	
}

function is_hit(){
    var rand = Math.floor(Math.random()*10)+1;
    return rand === 8;
}

function dojumpcode(obj)
{
	var stockcode = $("#"+obj).val();
	if (stockcode == '')
	{
		alert("\u8bf7\u8f93\u5165\u4ee3\u7801/\u7b80\u79f0\uff01");  //请输入代码/简称！
		return;
	}
	var len = stockcode.length;
	
	if (investor_url.substring(investor_url.length-1) == '/')
		investor_url = investor_url.substring(0,investor_url.length-1);

	if (len != 6)
	{
		var winpop = window.open(appUrl+'/api/tools/search_code_byname/'+encodeURIComponent(stockcode), '');
		winpop.focus();
	}else{
		var suffix = "";
        if (stockcode.substr(0,1)=='0' || stockcode.substr(0,1)=='3' )
            suffix = "sz";
        else
            suffix = "sh";
       

		var winpop = window.open("https://stockdata.cnstock.com/stock/"+suffix+stockcode.substr(0,6)+".html", '');
		winpop.focus();
	}
}

function dojumpcodegg(obj)
{
	var stockcode = $("#"+obj).val();
	if (stockcode == '')
	{
		alert("\u8bf7\u8f93\u5165\u4ee3\u7801/\u7b80\u79f0\uff01");  //请输入代码/简称！
		return;
	}
	var len = stockcode.length;
	
	if (investor_url.substring(investor_url.length-1) == '/')
		investor_url = investor_url.substring(0,investor_url.length-1);

	if (len != 6)
	{
		var winpop = window.open(appUrl+'/api/tools/search_code_byname/'+encodeURIComponent(stockcode), '');
		winpop.focus();
	}else{
		var winpop = window.open('https://irm.cnstock.com/ivlist_company/index/'+stockcode+'/gsgg/', '');
		winpop.focus();
	}
}

$(document).ready(function() { 
    
	//隐藏上证投资家
	//if( $('.nav-inner li').eq(6).find('a').html() === '\u4e0a\u8bc1\u6295\u8d44\u5bb6\u4ff1\u4e50\u90e8' ){
	//     $('.nav-inner li').eq(6).hide();
	//}
        
	var stockinput2 = $("#suggest2");
	if(typeof(stockinput2) != 'undefined' && stockinput2.length>0){
		$("#suggest2").autocomplete('https://companyapi.cnstock.com/companyAPI/getCompanyInfocode.do', {
			width:170,
			scrollHeight:300,
			multiple: false,
			matchContains: true,
			dataType: 'jsonp',
			cacheLength: 0,
			parse:function(data){
				var parsed = [];
				$.each(data, function(i,d){
					if(i=='rst'){
						for (var i = 0; i < d.length; i++) {
							parsed[parsed.length] = {
								data: "<span class=\"srsl\">" +d[i][1]+ "</span><span class=\"srsl\">" +d[i][2]+ "</span><span class=\"srsl\">" +d[i][3]+ "</span>",
								value: d[i][1],
								result: d[i][1].replace(/(<.+?>)/gi, '')
							};
						}
					}
				});
				return parsed;
			},
			formatItem: function(row, i, n) {
				return row;     
			}
		});
                
		$("#suggest2").result(function(event, data, formatted) {
			var hidden = $(this).parent().next().find(">:input");
			hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
			dojumpcode2();
		});
	}
});

function dojumpcode2()
{
	var stockcode = $("#suggest2").val();
	var len = stockcode.length;
	if (len != 6)
	{
		//错误的股票代码！
		alert("\u9519\u8bef\u7684\u80a1\u7968\u4ee3\u7801\uff01");	
		return false;
	}
}

function Commend(id,obj){
	if (obj == 'inquiry_commend_')
		api_url = appUrl+'/ask/company/commend/?inquiry_id='+id;
	else if (obj == 'weiquan_commend_')
		api_url = appUrl+'/ask/company/commend/?weiquan_id='+id;
    else if (obj=='topic_commend_count')
        api_url = appUrl+'/ask/company/commend/?topic_id='+id;
	else
		api_url = appUrl+'/ask/company/commend/?id='+id;

	$.ajax({
		type: 'get',
		url: api_url,	
		dataType: 'jsonp',
		jsonp: 'callback',
		data: {},
		async: false,
		success: function(data){
			alert(data.msg);
			if (data.code == 1) {
				var count = parseInt(document.getElementById(obj+id).innerHTML);
				document.getElementById(obj+id).innerHTML = count+1;
			}
		}
	}) 
}

function Flower(id,uid,obj){
	api_url = appUrl+'/ask/company/flower/?tid='+id+'&uid='+uid;

	$.ajax({
		type: 'get',
		url: api_url,	
		dataType: 'jsonp',
		jsonp: 'callback',
		data: {},
		async: false,
		success: function(data){
			alert(data.msg);
			if (data.code == 1) {
				var count = parseInt(document.getElementById(obj+id).innerHTML);
				document.getElementById(obj+id).innerHTML = count+1;
			}
		}
	}) 
}

function Egg(id,obj){
	api_url = appUrl+'/ask/company/egg/?tid='+id;

	$.ajax({
		type: 'get',
		url: api_url,	
		dataType: 'jsonp',
		jsonp: 'callback',
		data: {},
		async: false,
		success: function(data){
			alert(data.msg);
			if (data.code == 1) {
				var count = parseInt(document.getElementById(obj+id).innerHTML);
				document.getElementById(obj+id).innerHTML = count+1;
			}
		}
	}) 
}

function hasLogin(){
	$.ajax({
		type: 'get',
		url: appUrl+'/ask/company/',
		dataType: 'jsonp',
		jsonp: 'callback',
		data: {},
		async: false,
		success: function(data){
			alert(data.msg);
		}
	}) 
}
		
function CompanyAsk() {
	var content = document.getElementById('content');
	var suggest2 = document.getElementById('suggest2');
	if (suggest2.value == '')
	{
		alert('\u8bf7\u8f93\u5165\u4e0a\u5e02\u516c\u53f8\u80a1\u7968\u4ee3\u7801\uff01');
		suggest2.focus();
		return;
	}
	if (content.value == '')
	{
		alert('\u8bf7\u8f93\u5165\u95ee\u9898\uff01');
		content.focus();
		return;
	}
	
	$.ajax({
		type: 'get',
		url: appUrl+'/ask/company/ask_submit/',
		dataType: 'jsonp',
		jsonp: 'callback',
		data: {"stockcode":$("#suggest2").val(),"syncto_weibo":$("#syncto_weibo").val(),"content":$("#content").val()},
		async: false,
		success: function(data){
			alert(data.msg);
			content.value = '';
			if (data.code == 1)
				window.location.reload();
		}
	}) 
}
		
function StockOnlineAsk(obj,obj_uid) {
	var content = document.getElementById(obj);
	var uid = document.getElementById(obj_uid);
	if (content.value == '')
	{
		alert('\u8bf7\u8f93\u5165\u95ee\u9898\uff01');
		content.focus();
		return;
	}
	
	$.ajax({
		type: 'get',
		url: '/stock_online/ask_submit/',
		dataType: 'jsonp',
		jsonp: 'callback',
		data: {"content":content.value,"uid":uid.value},
		async: false,
		success: function(data){
			alert(data.msg);
			if (data.code == 1)
			{
				content.value = '';
				document.getElementById('askFrame').src='/stock_online/ask_form/';
			}
		}
	}) 
}

function checkNickname()
{
	var nickname = document.getElementById('nickname');
	if (nickname.value == '')
	{
		alert('\u8bf7\u8f93\u5165\u7528\u6237\u6635\u79f0\uff01');
		nickname.focus();
		return false;
	}
	return true;
}

function addFavourite2(id,type)
{
	if (id==null || id=='' || typeof(id)=='undefined')
		id = gather_get_obj_id();
	var obj = '';
	if (type == '') type = 'qmt';
	var title = encodeURIComponent(gather_title_for_licaichanpin());
	if (type == 'qmt')
		title = encodeURIComponent(gather_get_title());
	var url = encodeURIComponent(window.location.href);
	addFavourite(id,obj,type,title,url);
}

function addFavourite(id,obj,type,title,url){
	var api_url = appUrl+'/ask/company/addfavorite/?ask_id='+id;

	if (type == 'qmt')
		api_url = appUrl+'/ask/company/addfavorite/?qmt_id='+id+'&url='+url+'&title='+title;
	else if (type == 'inquiry')
		api_url = appUrl+'/ask/company/addfavorite/?inquiry_id='+id+'&title='+title;
	else if (type == 'weiquan')
		api_url = appUrl+'/ask/company/addfavorite/?weiquan_id='+id+'&title='+title;
	else if (type == 'weibo')
		api_url = appUrl+'/api/weibo/favourite/?weibo_tid='+id;
	else if (type == 'corpcms' || type == 'juchao')
		api_url = appUrl+'/ask/company/addfavorite/?type='+type+'&id='+id+'&url='+url+'&title='+title;

	$.ajax({
		type: 'get',
		url: api_url,
		dataType: 'jsonp',
		jsonp: 'callback',
		data: {},
		async: false,
		success: function(data){
			if (data.code == 1) {
				alert(data.msg);
				var count_span = $("#"+obj+id);
				if (count_span != null && typeof(count_span) != 'undefined' && count_span.innerHTML != ''){
					var count = parseInt(document.getElementById(obj+id).innerHTML);
					document.getElementById(obj+id).innerHTML = count+1;
				}
			}else if(data.code == -2){  //未登录
				if(window.confirm("\u60a8\u5c1a\u672a\u767b\u5f55\uff01\u7acb\u523b\u767b\u5f55\uff1f")){
					cnstockLogin();
				}
				return false;
			}else if(data.code == 0){    //未审核
				alert(data.msg);
				return false;
			}
		}
	}) 
}

function addFllow(uid){
    $.ajax({
        type: 'get',
        url: appUrl+'/api/weibo/follow?uid='+uid,
        dataType: 'jsonp',
        jsonp: 'cb',
        success: function(data){
                if (data.error == -2) {
                        alert("\u8bf7\u5148\u767b\u5f55");
                }else if(data.error == 0){
                        alert("\u5173\u6ce8\u6210\u529f");
                }
        }
    });
}

function gather_title_for_licaichanpin()
{
	var title = document.title;
	if (title.indexOf('-') > -1)
		title = title.substring(0,title.indexOf('-'));
	return title;
}

function Forward4QmtAndCommon()
{
	var obj = '';
	var url = encodeURIComponent(window.location.href);
	var id = gather_get_obj_id_for_forward();
	if (id.indexOf('.') > -1)
	{
		id = id.substring(0,id.indexOf('.'));
		title = encodeURIComponent(gather_get_title());
		type = 'qmt';
	}
	else
	{
		title = encodeURIComponent(gather_title_for_licaichanpin());
		type = 'common';
	}
	
	Forward(id,obj,type,title,url);
}

function sendTopic(text){
    var api_url = appUrl+'/api/weibo/add/?text='+encodeURIComponent(text);
    $.ajax({
        type: 'get',
        url: api_url,
        dataType: 'jsonp',
        jsonp: 'cb',
        success: function(data){
            if( data.error == -1 ){
                if( window.confirm('\u60a8\u5c1a\u672a\u767b\u5f55\uff0c\u7acb\u523b\u767b\u5f55\uff1f') ){
                    cnstockLogin();
                    return FALSE;
                }
            }else{
                alert('\u53d1\u8868\u6210\u529f');
                window.location.reload();
            }
            return false;
        }
     });
     
    return false;
}
		
//模拟转发，实际发帖
function Forward(id,obj,type,title,url)
{
	var api_url = appUrl+'/api/weibo/forward/?ask_id='+id;

	if (type == 'comment'){
		api_url = appUrl+'/api/weibo/forward/?comment_id='+id;
	}else if (type == 'qmt'){
		api_url = appUrl+'/api/weibo/forward/?qmt_id='+id+'&url='+url+'&title='+title;
	}else if (type == 'inquiry'){
		api_url = appUrl+'/api/weibo/forward/?inquiry_id='+id+'&title='+title;
	}else if (type == 'weiquan'){
		api_url = appUrl+'/api/weibo/forward/?weiquan_id='+id+'&title='+title;
	}else if (type == 'weibo'){
		api_url = appUrl+'/api/weibo/forward/?tid='+id;
	}else if (type == 'common'){
		api_url = appUrl+'/api/weibo/forward/?url='+url+'&title='+title;
	}

	$.ajax({
		type: 'get',
		url: api_url,
		dataType: 'jsonp',
		jsonp: 'cb',
		success: function(data){
			if (data.error == -1) {   //not audit
				alert("\u62b1\u6b49\uff0c\u60a8\u8981\u8f6c\u53d1\u7684\u5185\u5bb9\u8fd8\u672a\u5ba1\u6838\u901a\u8fc7\u3002");
				return false;
			}else if (data.error == -2) {  //not login
				if(window.confirm("\u60a8\u5c1a\u672a\u767b\u5f55\uff01\u7acb\u523b\u767b\u5f55\uff1f")){
					cnstockLogin();
				}
				return false;
			}else if (data.error == -3) {  
				alert("\u8f6c\u53d1\u5931\u8d25\uff01");
				return false;
			}else if(data.error==0){
				alert("\u5df2\u6210\u529f\u8f6c\u53d1\u81f3\u60a8\u7684\u5fae\u535a\uff01");
				var count_span = document.getElementById(obj+id);
				if (count_span != null && typeof(count_span) != 'undefined' && count_span.innerHTML != ''){
						var count = parseInt(count_span.innerHTML);
						count_span.innerHTML = count+1;
				}
				return false;
			}
		}
	});
}
		
//真正的转发
function trueForward(tid,obj){
	$.ajax({
		type: 'get',
		url: appUrl+'/api/weibo/forward?tid='+tid,
		dataType: 'jsonp',
		jsonp: 'cb',
		success: function(data){
			if (data.error == -2) {
				if(window.confirm("\u60a8\u5c1a\u672a\u767b\u5f55\uff01\u7acb\u523b\u767b\u5f55\uff1f")){
					cnstockLogin();
				}
				return false;
			}else if(data.error==0){
				alert("\u5df2\u6210\u529f\u8f6c\u53d1\u81f3\u60a8\u7684\u5fae\u535a\uff01");
				var count = parseInt(document.getElementById(obj+tid).innerHTML);
				document.getElementById(obj+tid).innerHTML = count+1;
				return false;
			}
		}

	});
}

function viewNewAsk()
{
	document.getElementById('askFrame').src='/stock_online/ask_form/';
}
		
function searchStockOnlineAsk()
{
	var date = document.getElementById('J_datepicker');
	if (date.value == ''){
		alert('\u8bf7\u9009\u62e9\u65e5\u671f\uff01');
		date.focus();
		return;
	}
	document.getElementById('askFrame').src='/stock_online/ask_form/'+date.value+'/';
}

function searchStockOnlineKeywords()
{
	var keywords = document.getElementById('keywords');
	if (keywords.value == ''){
		alert('\u8bf7\u8f93\u5165\u5173\u952e\u5b57\uff01');
		keywords.focus();
		return;
	}
	
	var date = document.getElementById('J_datepicker').value;
	if (date == '') date = 'all';
	document.getElementById('askFrame').src='/stock_online/ask_form/'+date+'/'+(keywords.value)+'/';
}

var totalPage = parseInt($("#hid_totalPage").val());
var currentPageId = "IDNewsDtail";
if(totalPage && totalPage > 1){
	var docId = $("#hid_docId").val();
	var $div0=$("#doc_"+docId+"_0");
	$("div[id^='doc_"+docId+"_']").each(function(index, domObj){if(index>0){$(this).insertAfter($div0);$div0 = $(this);}});
	var page=0;
	var param=location.search.match(new RegExp("[\?&]page=([^&]*)(&?)", "i"));
	if(param && !isNaN(param[1])){page = parseInt(param[1])-1;}
	if(page<0)page=0;
	if(page>=totalPage)page=totalPage-1;
	currentPageId = "doc_"+docId+"_"+page;
	try{
		$("div[id^='doc_"+docId+"_']").hide();
		$("#"+currentPageId).show();
		if(page==0)
			$("#span_previous_page").hide();
		if(page==(totalPage-1))
			$("#span_next_page").hide();
		var $spanPageNumbers = $("span[id^='span_pagerNumber_']").removeClass().addClass("otherPages");
		$("#span_pagerNumber_" + page).removeClass().addClass("currentPage");
		$spanPageNumbers.each(function(){
			$(this).html(($(this).html().replace(/[^0-9]/g,"")));
		});
		if(totalPage>11){
			$spanPageNumbers.each(function(i, ele){
				if(i<page-5 || i>page+5)$(ele).hide();
			});
		}else{
			$("#span_total_page").hide();
		}
	}catch(e){}
}

function getQueryString(name)
{
    if(location.href.indexOf("?")==-1 || location.href.indexOf(name+'=')==-1)
    {
        return '';
    }
 
    var queryString = location.href.substring(location.href.indexOf("?")+1);
 
    var parameters = queryString.split("&");
 
    var pos, paraName, paraValue;
    for(var i=0; i<parameters.length; i++)
    {
        pos = parameters[i].indexOf('=');
        if(pos == -1) { continue; }
 
        paraName = parameters[i].substring(0, pos);
        paraValue = parameters[i].substring(pos + 1);
 
        if(paraName == name)
        {
            return unescape(paraValue.replace(/\+/g, " "));
        }
    }
    return '';
};
 

function goPage(p){
    
	var targetPage = parseInt(p)+1;
        var thisPage = getQueryString('page');
        
	if(thisPage){
            
            window.location.href = window.location.href.replace('page='+thisPage,'page='+targetPage);
            return false;
            
	}else{
            
            if(window.location.href.indexOf('?') > 0 ){
                window.location.href = window.location.href += "&page=" + targetPage;
            }else{
                window.location.href = window.location.href += "?page=" + targetPage;
            }
            
	}
}

function checkNavSearch()
{
	var keywords = navsearch_form.q;
	if (navsearch_form.c.value == 8)
		keywords = navsearch_form.searchChange;

	//请输入关键字！
	if (keywords.value == '') {
		alert("\u8bf7\u8f93\u5165\u5173\u952e\u5b57\uff01");
		keywords.focus();
		return false;
	}
	
	if (navsearch_form.c.value == 8)
	{
		dojumpcode('J_search_input');
		return false;
	}
	
	return true;
}

function searchKeyword(keyword)
{
//	navsearch_form.q.value = keyword;
//	navsearch_form.c.value = 1;
//	navsearch_form.submit();
	window.open('https://search.cnstock.com/go.aspx?q='+keyword);
}

function checkTousu2()
{
	if (tsform2.nickname.value == '') {
		alert("\u8bf7\u8f93\u5165\u59d3\u540d\uff01");
		tsform2.nickname.focus();
		return false;
	}
	else if (tsform2.phone.value == '') {
		alert("\u8bf7\u8f93\u5165\u8054\u7cfb\u7535\u8bdd\uff01");
		tsform2.phone.focus();
		return false;
	}
	else if (checkPhoneFormat(tsform2.phone.value) == false) {
		alert("\u8054\u7cfb\u7535\u8bdd\u683c\u5f0f\u4e0d\u6b63\u786e\uff01");
		tsform2.phone.focus();
		return false;
	}
	else if (tsform2.email.value == '') {
		alert("\u8bf7\u8f93\u5165\u7535\u5b50\u90ae\u7bb1\uff01");
		tsform2.email.focus();
		return false;
	}
	else if (checkEmailFormat(tsform2.email.value) == false) {
		alert("\u7535\u5b50\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e\uff01");
		tsform2.email.focus();
		return false;
	}
	else if (tsform2.content.value == '') {
		alert("\u8bf7\u8f93\u5165\u6295\u8bc9\u5185\u5bb9\uff01");
		tsform2.content.focus();
		return false;
	}
	return true;
}

function checkTousu1()
{
	if (tsform1.phone.value == '') {
		alert("\u8bf7\u8f93\u5165\u8054\u7cfb\u7535\u8bdd\uff01");
		tsform1.phone.focus();
		return false;
	}
	else if (tsform1.content.value == '') {
		alert("\u8bf7\u8f93\u5165\u6295\u8bc9\u5185\u5bb9\uff01");
		tsform1.content.focus();
		return false;
	}
	return true;
}

function cnstock_add_stock( stock_code ){
    
    $.ajax({
        type:'GET',
        url:appUrl+'/api/stock/add_stock',
        data:{
            stock_code:stock_code,
            encode:'gbk'
        },
        dataType:'jsonp',
        jsonp:'cb',
        success:function(data){
            if(data.error ==0 ){
                alert('\u6dfb\u52a0\u6210\u529f');
                return false;
            }else if (data.error==-3){
                alert('\u60a8\u5c1a\u672a\u767b\u5f55\uff0c\u8bf7\u5148\u767b\u5f55\uff01');
                return false;
            }else{
                alert(data.resultMSG);
                return false;
            }
        }
    });
    
}

$('.webchat-block').on({
	mouseover:function(){
		$(this).find('.webchat-img').show();
	},
	mouseout:function(){
		$(this).find('.webchat-img').hide();
	}
});

SZB.productCompare = function(){
	var container = $('#J_product_compare');
	var url = "https://caifu.cnstock.com/product/fxdb/"

	this.checkLen = function(e){
		if(container.find('input[type="checkbox"]:checked').length > 4){
			e.preventDefault();
			alert('\u6700\u591a\u53ea\u80fd\u540c\u65f6\u9009\u62e94\u6b3e\u4ea7\u54c1\u8fdb\u884c\u6bd4\u8f83~')
		}
	};

	this.submitCompare = function(e){
		e.preventDefault();
		var compare = [];
		var items = container.find('input[type="checkbox"]:checked');
		for(var i = 0; i < items.length ; i ++){
			compare.push(items.eq(i).attr('data-cid'));
			if(i == 4) break;
		}
		compare = compare.join(',');
		//console.log(compare);
		if(compare_type){
			window.open(url + compare_type + '?code=' + compare);
		}
	};

	container.on('click','input[type="checkbox"]',$.proxy(this.checkLen,this));
	container.on('click','.btn',$.proxy(this.submitCompare,this));

};

SZB.productCompare();

var goupshare = function() {
    var _this = this;
    var vWidth = $(window).width();
    var vHeight = $(window).height();
    var left = vWidth / 2 + 505;
    var top = vHeight / 2 - 95;
    var oDiv, oGoup, oSina, oTencent;
    this.setInit = function() {
        oDiv.css("left", left);
        oDiv.css("top", top);
        $(window).scroll(function() {
            scrollTop = $(window).scrollTop();
            if (scrollTop > 100) {
                oGoup.fadeIn("normal");
            } else if (scrollTop <= 100) {
                oGoup.fadeOut("normal");
            }
        });
        oGoup.on("click", function() {
            $("html,body").animate({
                scrollTop: 0
            },
            200);
        });
        oSina.on("click", function() {
            window.open('https://v.t.sina.com.cn/share/share.php?title=' + encodeURIComponent(document.title) + '&url=' + encodeURIComponent(location.href) + '&source=cnstock');
        });
        oTencent.on("click", function() {
            window.open('https://v.t.qq.com/share/share.php?title=' + encodeURIComponent(document.title) + '&url=' + encodeURIComponent(location.href) + '&site==https://www.cnstock.com');
        });
    };
    if ($(".goup-share").length == 1) {
        oDiv = $(".goup-share");
        oGoup = oDiv.find(".goup");
        oSina = oDiv.find(".sina");
        oTencent = oDiv.find(".tencent");
        this.setInit();
    };
}();